HackerRank Sam and substrings
https://www.hackerrank.com/challenges/sam-and-substrings/problem
提出
11/13 test cases failed :(
code: python
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'substrings' function below.
#
# The function is expected to return an INTEGER.
# The function accepts STRING n as parameter.
#
def substrings(n):
# Write your code here
ans = 0
for i in range(1, len(n)+1):
for j in range(len(n)+1 - i):
ans += int(nj:j+i)
return ans
if __name__ == '__main__':
fptr = open(os.environ'OUTPUT_PATH', 'w')
n = input()
result = substrings(n)
fptr.write(str(result) + '\n')
fptr.close()
解答
code: python
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'substrings' function below.
#
# The function is expected to return an INTEGER.
# The function accepts STRING n as parameter.
#
def substrings(s):
# Write your code here
MOD = 1000000007
n = len(s)
a = 0 * n # weight
b = 0 * n # weight sum
a0 = 1
b0 = 1
for i in range(1, n):
ai = (10 * ai-1) % MOD
bi = (bi-1 + ai) % MOD
# 16 -> print(a, b) 1, 10 1, 11
ans = 0
for i in range(n):
ans += (int(si) * bn-i-1 * (i+1)) % MOD
# 1 * 11 * 1 + 6 * 1 * 2
# print(ans) 16 -> 11, 23
ans %= MOD
return ans
if __name__ == '__main__':
fptr = open(os.environ'OUTPUT_PATH', 'w')
n = input()
result = substrings(n)
fptr.write(str(result) + '\n')
fptr.close()
メモ
https://www.youtube.com/watch?v=kXS66eP0T6s
https://scrapbox.io/files/61d24a828b5049001df7341f.png